package com.ymatou.iapi.livebuyer.testcase;

import java.util.List;
import java.util.Map;
import java.util.Random;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.livebuyer.parameter.QueryTopProductBean;
import com.ymatou.iapi.livebuyer.service.QueryTopProductCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
/**
*查询置顶商品
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryTopProduct{
	private static QueryTopProductBean querytopproductBean;
	private static QueryTopProductCall querytopproductCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询置顶商品");
	}

	@Before
	public void caseUp() {
		querytopproductBean=new QueryTopProductBean();
		querytopproductCall=new QueryTopProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(querytopproductCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryTopProduct_001() {
		Logger.start(true,"查询置顶商品成功");
		try {
			//只有selliveproduct.action 是0（未删除）  且 status是1（在售）的商品露出
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(uinfo.getUserId());
			String pid =ProductManagerCallServiceV3.AddSellerProduct(addpro);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			LiveRoomCallService.SetLiveProductTop(createlive.getId(), pid);
			Integer liveid=createlive.getId();
			querytopproductBean.setLiveId(liveid);
			querytopproductCall.setData(querytopproductBean);
			querytopproductCall.callService();			
			Logger.verifyEquals("200", querytopproductCall.getString("code"), "判断code是否为200");
			JsonObject data = querytopproductCall.getJsonObject().get("data").getAsJsonObject();
			Logger.verifyEquals(pid, data.get("productId").getAsString(), "判断置顶商品");
			Logger.verifyEquals("true", data.get("top").getAsString(), "判断置顶商品");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_QueryTopProduct_002() {
		Logger.start(true,"不输入liveId失败");
		try {
			querytopproductCall.setData(querytopproductBean);
			querytopproductCall.callService();			
			Logger.verifyEquals("100", querytopproductCall.getString("code"), "判断code");
			Logger.verifyIsContains("错误的请求参数", querytopproductCall.getString("message"), "判断message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_QueryTopProduct_003() {
		Logger.start(true,"liveId错误失败");
		try {
			querytopproductBean.setLiveId(-1);
			querytopproductCall.setData(querytopproductBean);
			querytopproductCall.callService();			
			Logger.verifyEquals("100", querytopproductCall.getString("code"), "判断code");
			Logger.verifyIsContains("错误的请求参数", querytopproductCall.getString("message"), "判断message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryTopProduct_004() {
		Logger.start(true,"移除商品不显示置顶");
		try {
			//只有selliveproduct.action 是0（未删除）  且 status是1（在售）的商品露出
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(uinfo.getUserId());
			String pid =ProductManagerCallServiceV3.AddSellerProduct(addpro);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			LiveRoomCallService.SetLiveProductTop(createlive.getId(), pid);
			LiveRoomCallService.RemoveLiveProduct(createlive.getId(), pid);
			Integer liveid=createlive.getId();
			querytopproductBean.setLiveId(liveid);
			querytopproductCall.setData(querytopproductBean);
			querytopproductCall.callService();			
			Logger.verifyEquals("200", querytopproductCall.getString("code"), "判断code是否为200");
			Logger.verifyEquals("null", querytopproductCall.getString("data"), "判断无置顶");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryTopProduct_005() {
		Logger.start(true,"暂停销售商品不显示置顶");
		try {
			//只有selliveproduct.action 是0（未删除）  且 status是1（在售）的商品露出
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(uinfo.getUserId());
			String pid =ProductManagerCallServiceV3.AddSellerProduct(addpro);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			LiveRoomCallService.SetLiveProductTop(createlive.getId(), pid);
			LiveRoomCallService.SuspendLiveProduct(createlive.getId(), pid);
			Integer liveid=createlive.getId();
			querytopproductBean.setLiveId(liveid);
			querytopproductCall.setData(querytopproductBean);
			querytopproductCall.callService();			
			Logger.verifyEquals("200", querytopproductCall.getString("code"), "判断code是否为200");
			Logger.verifyEquals("null", querytopproductCall.getString("data"), "判断无置顶");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}